/****************************************************************************
 *
 *                Admin Over IM
 *
 ****************************************************************************
 *
 *  <Copyright>
 *    Copyright (C) 2008  Basov Alexander
 *
 *  <Unit> iq.cpp
 *   <ImplementationNotes>
 *   <PortabilityIssues>
 *   <AnticipatedChanges>
 *****************************************************************************
 *
 *  $Revision: 130 $ $Author: coopht $
 *  $Date: 2008-03-11 18:20:48 +0000 (Tue, 11 Mar 2008) $
 *****************************************************************************/
#include <iostream>
#include <algorithm>
#include <cctype>

#include <libxml/tree.h>

#include "iq.h"

using namespace std;

 string toLowerCase (string str)
{
  string result = "";
  transform (str.begin (), str.end (), result.begin (), _tolower); 
  return result;
}

void err (const string msg)
{
  cout << msg << endl;
  exit (EXIT_FAILURE);
}

IQ::IQ (IQ_TYPE _type,
	const std::string _id , 
	const std::string _from,
	const std::string _to)
{
    this->type = _type;
    this->id = _id;
    this->fromJID = _from;
    this->toJID = _to;
}
  

const string IQ::getTypeStr ()
{
  switch (this->type)
  {
    case GET:
      return "get";
      break;
    case SET:
      return "set";
      break;
    case RESULT:
      return "result";
      break;
    case ERROR:
      return "error";
      break;
    default:
      return "";
      break;
  }
}

void IQ::setTypeStr (const std::string val)
{
  string tmp = toLowerCase (val);
  if (tmp == "get")
    this->type = GET;
  else if (tmp == "set")
    this->type = SET;
  else if (tmp == "result")
    this->type = RESULT;
  else if (tmp == "error")
    this->type = ERROR;
}

// IQAuthRequest Class

const std::string IQAuthRequest::toXmlStr ()
{
  return ("<iq type='" + getTypeStr () + 
	  "' id='" + getId () + 
	  "' to='" + getToJID () + "'>" +
	  "<query xmlns='jabber:iq:auth'>" +
	  "<username>" + getUserName () + "</username>" +
	  "<password>" + getPassword () + "</password>" + 
	  "<resource>" + getResource () + "</resource>" +
	  "</query></iq>");
}

